/**
 * Created by wuhaiyang on 2017/4/7.
 */


import ArticleModel from './ArticleModel'
export default class ArticleCrawlerUtil {

    constructor() {
        if (typeof ArticleCrawlerUtil.instance === 'object') {
            return ArticleCrawlerUtil.instance
        }
        ArticleCrawlerUtil.instance = this;
    }

    fetchArticle(url) {
        return new Promise((resolve, reject) => {
            fetch(url)
                .then((response) => response.text())
                .catch(error => {
                    console.log(error)
                })
                .then((response) => {
                    try {
                        resolve(this.htmlToRepo(response))
                    } catch (e) {
                        reject(e)
                    }
                })
        })
    }

    htmlToRepo(html) {
        // console.log(html)
        // var content = $('.ui_very_relaxed_divided_unstackable_items')
        let contentArray = html.split('<a class="ui small image item-cover"')
        contentArray.shift()
        let articleArray = []
        contentArray.map((item, index, array) => {
            let model = new ArticleModel()
            // href
            let hrefWrap = item.substring(0, item.indexOf('>')).replace(/[(^\s*)|(\s*$)]/g, "")
            let flag1 = 'href="'
            model.href = hrefWrap.substring(hrefWrap.indexOf(flag1) + flag1.length, hrefWrap.length - 1)

            // picUrl
            let imgWrap = item.substring(item.indexOf('>') + 1, item.indexOf('class="lazy">')).replace(/[(^\s*)|(\s*$)]/g, "")
            let flag2 = '<img data-original="';
            model.picUrl = imgWrap.substring(flag2.length - 1, imgWrap.length - 1)

            //title
            let flag3 = 'a class="header" '
            let flag4 = '<div class="description">'
            let titleWrapper = item.substring(item.indexOf(flag3) + flag3.length, item.indexOf(flag4))
            let flag3_flag1 = 'target="_blank">'
            let flag3_flag2 = '</a>'
            model.title = titleWrapper.substring(titleWrapper.indexOf(flag3_flag1) + flag3_flag1.length, titleWrapper.indexOf(flag3_flag2)).replace(/[(^\s*)|(\s*$)]/g, "")

            //description
            let des = item.substring(item.indexOf(flag4) + flag4.length, item.indexOf('</p>')).replace(/[(^\s*)|(\s*$)]/g, "")
            let flag5 = '<p>'
            model.description = des.substring(flag5.length, des.length)

            //readCount
            let flag6 = '<div class="item">'
            let str = item.substring(item.indexOf(flag6) + flag6.length, item.length).replace(/[(^\s*)|(\s*$)]/g, "")
            model.readCount = str.substring('<iclass="unhideicon"></i>'.length, str.indexOf('</div>'))

            //stars
            let flag7 = '<iclass="likeicon"></i>'
            let starWrapper = str.substring(str.indexOf(flag7) + flag7.length, str.length)
            model.likeCount = starWrapper.substring(0, starWrapper.indexOf('</div>'))


            articleArray.push(model)
        })
        return articleArray
    }
}